import axios from "axios";
import { Message, Loading } from "element-ui";
let loading = null;

function getOS() {
  var sUserAgent = navigator.userAgent;
  var isWin = navigator.platform == "Win32" || navigator.platform == "Windows";
  var isMac =
    navigator.platform == "Mac68K" ||
    navigator.platform == "MacPPC" ||
    navigator.platform == "Macintosh" ||
    navigator.platform == "MacIntel";
  if (isMac) return "Mac";
  var isUnix = navigator.platform == "X11" && !isWin && !isMac;
  if (isUnix) return "Unix";
  var isLinux = String(navigator.platform).indexOf("Linux") > -1;
  if (isLinux) return "Linux";
  if (isWin) {
    var isWin2K =
      sUserAgent.indexOf("Windows NT 5.0") > -1 ||
      sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "Win2000";
    var isWinXP =
      sUserAgent.indexOf("Windows NT 5.1") > -1 ||
      sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "WinXP";
    var isWin2003 =
      sUserAgent.indexOf("Windows NT 5.2") > -1 ||
      sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "Win2003";
    var isWinVista =
      sUserAgent.indexOf("Windows NT 6.0") > -1 ||
      sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "WinVista";
    var isWin7 =
      sUserAgent.indexOf("Windows NT 6.1") > -1 ||
      sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "Win7";
    var isWin10 =
      sUserAgent.indexOf("Windows NT 10") > -1 ||
      sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "Win10";
  }
  return "other";
}
function browse() {
  var browser = {};
  var userAgent = navigator.userAgent.toLowerCase();
  var s;
  (s = userAgent.match(/msie ([\d.]+)/))
    ? (browser.ie = s[1])
    : (s = userAgent.match(/firefox\/([\d.]+)/))
    ? (browser.firefox = s[1])
    : (s = userAgent.match(/chrome\/([\d.]+)/))
    ? (browser.chrome = s[1])
    : (s = userAgent.match(/opera.([\d.]+)/))
    ? (browser.opera = s[1])
    : (s = userAgent.match(/version\/([\d.]+).*safari/))
    ? (browser.safari = s[1])
    : 0;
  var version = "";
  if (browser.ie) {
    version = "IE " + browser.ie;
  } else {
    if (browser.firefox) {
      version = "Firefox " + browser.firefox;
    } else {
      if (browser.chrome) {
        version = "Chrome " + browser.chrome;
      } else {
        if (browser.opera) {
          version = "Opera " + browser.opera;
        } else {
          if (browser.safari) {
            version = "Safari " + browser.safari;
          } else {
            version = "未知浏览器";
          }
        }
      }
    }
  }
  return version;
}

// 假设你有一个方法来获取或设置 IP 和端口
function getApiUrl() {
  // 这里你可以根据环境变量、用户输入或其他逻辑来动态返回 IP 和端口
  const ip = localStorage.getItem("IP"); // 动态获取的 IP
  return `http://${ip}`;
}

const http = axios.create({
  baseURL: getApiUrl(),
  // baseURL: '/',
  timeout: 10000,
});
const RequestMode = ["delete", "get", "post", "put"];
http.interceptors.request.use((config) => {
  console.log("config111", config);

  config.baseURL = getApiUrl(); 
  loading = Loading.service({
    lock: true,
    text: "加载中……",
    background: "rgba(0, 0, 0, 0.7)",
  });
  console.log("config2222", config);

  // if (sessionStorage.getItem('ClientName')==null){
  //   sessionStorage.setItem('ClientName', getOS().toString()+'-'+browse().toString())
  // }
  // if(config.method.toLowerCase()==='get' && config.params!==undefined){
  //     config.params.ClientName = sessionStorage.getItem('ClientName')
  //     config.params.ClientIP = sessionStorage.getItem('ClientIP')
  // }else if (config.data!==undefined && RequestMode.indexOf(config.method.toLowerCase())!==-1){
  //   config.data.ClientName = getOS().toString()+'-'+browse().toString()
  // }
  return config;
});
http.interceptors.response.use(
  (config) => {
    loading.close();
    // if(config.data.ClientIP!==undefined &&
    //     sessionStorage.getItem('ClientIP')!==config.data.ClientIP &&
    //     config.data.ClientIP!==''){
    //   sessionStorage.removeItem('ClientIP')
    //   sessionStorage.setItem('ClientIP',config.data.ClientIP)
    // }
    return config.data;
  },
  (err) => {
    loading.close();
  }
);

export default http;
